package com.ddtech.business.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddtech.business.check.entity.CheckUserQuestion;
import com.ddtech.business.check.service.CheckService;
import com.ddtech.business.check.service.CheckUserQuestionService;
import com.ddtech.business.check.vo.res.QueAndOpt;
import com.ddtech.business.constant.BusinessEnum;
import com.ddtech.business.constant.Del;
import com.ddtech.business.practice.dto.QueAndOptDto;
import com.ddtech.business.practice.entity.PracticeUserQuestion;
import com.ddtech.business.practice.service.PracticeService;
import com.ddtech.business.practice.service.PracticeUserQuestionService;
import com.ddtech.business.web.entity.CollectRecord;
import com.ddtech.business.web.mapper.CollectRecordMapper;
import com.ddtech.business.web.query.CollectRecordQuery;
import com.ddtech.business.web.service.CollectRecordService;
import com.ddtech.exception.BizPreconditions;
import com.ddtech.utils.PageUtil;
import com.ddtech.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 收藏记录表 服务实现类
 * </p>
 *
 * @author bin_pu
 * @since 2023-02-06
 */
@Service
public class CollectRecordServiceImpl extends ServiceImpl<CollectRecordMapper, CollectRecord> implements CollectRecordService {
    @Autowired
    private CheckService checkService;
    @Autowired
    private CheckUserQuestionService checkUserQuestionService;
    @Autowired
    private PracticeService practiceService;
    @Autowired
    private PracticeUserQuestionService practiceUserQuestionService;

    @Resource
    private CollectRecordMapper collectRecordMapper;

    @Override
    public Object pageList(CollectRecordQuery collectRecordQuery) {
        //IPage<CollectRecord> page = this.page(collectRecordQuery.populatePage(), collectRecordQuery.autoWrapper());
        IPage<Map<String, Object>> page = collectRecordMapper.collectCourseList(collectRecordQuery.populatePage(), collectRecordQuery);
        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }

    @Override
    public Object pageListTm(CollectRecordQuery collectRecordQuery) {
        IPage<CollectRecord> page = this.page(collectRecordQuery.populatePage(), collectRecordQuery.autoWrapper());
        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }

    @Override
    public Object create(CollectRecord collectRecord) {
        CollectRecord collectRecordOld = this.getByBusId1(collectRecord.getBusId());
        if(collectRecordOld!=null){
            collectRecordOld.setIsDelete(0);
            collectRecordOld.init();
            collectRecordOld.setUserId(SecurityUtils.getUserId());
            collectRecordOld.setUserName(SecurityUtils.getName());
            this.saveOrUpdate(collectRecordOld);
            return collectRecordOld;
        }else{
            collectRecord.init();
            collectRecord.setUserId(SecurityUtils.getUserId());
            collectRecord.setUserName(SecurityUtils.getName());
            this.saveOrUpdate(collectRecord);
            return collectRecord;
        }
    }


    @Override
    public void del(String id) {
        CollectRecord collectRecord = this.getById(id);
        BizPreconditions.check(collectRecord == null, BusinessEnum.QUERY_ERROR.getMsg());
        collectRecord.baseDelete();
        this.saveOrUpdate(collectRecord);
    }

    public CollectRecord getByBusId1(String busId) {
        LambdaQueryWrapper<CollectRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CollectRecord::getBusId, busId);
        //新增当前登录人查询条件
        queryWrapper.eq(CollectRecord::getUserId, SecurityUtils.getUserId());
        return getOne(queryWrapper);
    }

    @Override
    public CollectRecord getByBusId(String busId) {
        LambdaQueryWrapper<CollectRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CollectRecord::getBusId, busId);
        queryWrapper.eq(CollectRecord::getIsDelete, Del.FALSE);
        //新增当前登录人查询条件
        queryWrapper.eq(CollectRecord::getUserId, SecurityUtils.getUserId());
        return getOne(queryWrapper);
    }

    @Override
    public void cancelCollect(String id) {
        CollectRecord collectRecord = getByBusId(id);
        if(collectRecord==null){
            collectRecord=this.getById(id);
        }
        BizPreconditions.check(collectRecord == null, BusinessEnum.QUERY_ERROR.getMsg());
        collectRecord.baseDelete();
        this.saveOrUpdate(collectRecord);
    }

    @Override
    public Object getQuestionPageList(CollectRecordQuery collectRecordQuery) {
        collectRecordQuery.setType(2);
        collectRecordQuery.setUserId(SecurityUtils.getUserId());
        IPage<CheckUserQuestion>  page = collectRecordMapper.collectCheckList(collectRecordQuery.populatePage(), collectRecordQuery);
//        IPage<CollectRecord> page = this.page(collectRecordQuery.populatePage(), collectRecordQuery.autoWrapper());
//        List<CollectRecord> records = page.getRecords();
//        List<QueAndOpt> typeQuestion = new ArrayList<>();
//        if (CollUtil.isNotEmpty(records)) {
//            List<String> ids = records.stream().map(CollectRecord::getBusId).collect(Collectors.toList());
//            LambdaQueryWrapper<CheckUserQuestion> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.in(CheckUserQuestion::getId, ids);
//            List<CheckUserQuestion> list = checkUserQuestionService.list(queryWrapper);
//            typeQuestion = checkService.getTypeQuestion(SecurityUtils.getUserId(), list);
//        }
        List<QueAndOpt> typeQuestion = new ArrayList<>();
        if(page.getTotal()>0){
            typeQuestion = checkService.getTypeQuestion(SecurityUtils.getUserId(), page.getRecords());
        }
        return PageUtil.toPage(typeQuestion, page.getTotal());
    }

    @Override
    public Object getBankPageList(CollectRecordQuery collectRecordQuery) {
        collectRecordQuery.setType(3);
        collectRecordQuery.setUserId(SecurityUtils.getUserId());
        IPage<PracticeUserQuestion> page = collectRecordMapper.collectBankList(collectRecordQuery.populatePage(), collectRecordQuery);
//        IPage<CollectRecord> page = this.page(collectRecordQuery.populatePage(), collectRecordQuery.autoWrapper());
//        List<CollectRecord> records = page.getRecords();
//        List<QueAndOptDto> typeQuestion = new ArrayList<>();
//        if (CollUtil.isNotEmpty(records)) {
//            List<String> ids = records.stream().map(CollectRecord::getBusId).collect(Collectors.toList());
//            LambdaQueryWrapper<PracticeUserQuestion> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.in(PracticeUserQuestion::getId, ids);
//            List<PracticeUserQuestion> list = practiceUserQuestionService.list(queryWrapper);
//            typeQuestion = practiceService.getTypeQuestion(SecurityUtils.getUserId(), list);
//        }
        List<QueAndOptDto> typeQuestion = new ArrayList<>();
        if(page.getTotal()>0){
            typeQuestion = practiceService.getTypeQuestion(SecurityUtils.getUserId(), page.getRecords());
        }
        return PageUtil.toPage(typeQuestion, page.getTotal());
    }

    /***
     * description: 查询个人收藏
     * @param:  * @param collectRecordQuery
     * @date: 2023-06-12
     * @return: java.lang.Object
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    @Override
    public Object personCollectCourseList(CollectRecordQuery collectRecordQuery) {
        collectRecordQuery.setUserId(SecurityUtils.getUserId());
        IPage<Map<String, Object>> page = collectRecordMapper.personCollectCourseList(collectRecordQuery.populatePage(), collectRecordQuery);
        List<Map<String, Object>> list= page.getRecords().stream().map((map) -> {
            Object obj=map.get("themeImage");
            if(obj!=null){
                JSONArray array = JSON.parseArray(obj.toString());
                if (array!=null && array.size()>0){
                    JSONObject json=array.getJSONObject(0);
                    map.put("themeImage",json.get("url"));
                }
            }
            return map;
        }).collect(Collectors.toList());
        return PageUtil.toPage(list, page.getTotal());
    }
}
